package com.tanhua.manage.listener;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserInfoApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired(required = false)
    UserInfoApi userInfoApi;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 过期key
        String expiredKey = message.toString();
        log.info(expiredKey + "过期了");
        // 下面可以通过redis工具获取值或者执行业务逻辑
        if(expiredKey.contains("_USERSTATUS_")){

            String[] strings = expiredKey.split("_");

            UserInfo userInfo = userInfoApi.findById(Long.valueOf(strings[2]));

            userInfo.setUserStatus("1");

            userInfoApi.update(userInfo);

        }

    }

    public RedisKeyExpirationListener(RedisMessageListenerContainer redisMessageListenerContainer ){
        super(redisMessageListenerContainer);
    }

}
